package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Common;
import com.example.elderlycare.dao.entity.Department;
import com.example.elderlycare.dao.entity.Employee;
import com.example.elderlycare.dao.entity.NursingHome;
import com.example.elderlycare.dao.mapper.CommonMapper;
import com.example.elderlycare.dao.mapper.DepartmentMapper;
import com.example.elderlycare.dao.mapper.EmployeeMapper;
import com.example.elderlycare.dao.mapper.NursingHomeMapper;
import com.example.elderlycare.service.EmployeeService;
import com.example.elderlycare.service.entity.EmployeeListItem;
import com.example.elderlycare.service.entity.EmployeeListResponseBody;
import com.example.elderlycare.utils.DateUtil;
import com.example.elderlycare.utils.FormatDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Resource
    EmployeeMapper employeeMapper;

    @Resource
    DepartmentMapper departmentMapper;

    @Resource
    NursingHomeMapper nursingHomeMapper;

    @Resource
    CommonMapper commonMapper;

    /**
     * queryEmployeeList 根据参数查询员工列表
     *
     * @param params 查询参数
     * @return EmployeeListResponseBody
     */
    @Override
    public EmployeeListResponseBody queryEmployeeList(Map<String, Object> params) {
        Integer count = employeeMapper.queryEmployeeCount(params);
        List<Employee> employeeList = employeeMapper.queryEmployeeList(params);
        EmployeeListResponseBody.EmployeeListResponseBodyBuilder employeeListResponseBodyBuilder = EmployeeListResponseBody.builder();
        employeeListResponseBodyBuilder.total(count);
        List<EmployeeListItem> employeeListItems = employeeList.stream().map(item -> {
            EmployeeListItem.EmployeeListItemBuilder employeeListItemBuilder = EmployeeListItem.builder();
            employeeListItemBuilder.id(item.getId()).
                    name(item.getName()).
                    number(item.getNumber()).
                    phone(item.getPhone()).
                    entryTime(FormatDate.getFormatDate(item.getEntryTime())).
                    sex(item.getSex());

            Department department = departmentMapper.selectById(item.getDepartmentId());
            employeeListItemBuilder.departmentId(item.getDepartmentId()).
                    department(department == null ? "未定义" : department.getName());

            Department father = departmentMapper.selectById(item.getFatherId());
            employeeListItemBuilder.fatherId(item.getFatherId()).
                    father(father == null ? "未定义" : father.getName());

            NursingHome nursingHome = nursingHomeMapper.selectById(item.getNurseId());
            employeeListItemBuilder.nurseId(item.getNurseId()).
                    nurse(nursingHome == null ? "未定义" : nursingHome.getName());

            Common cardType = commonMapper.selectById(item.getCardTypeId());
            employeeListItemBuilder.cardTypeId(item.getCardTypeId()).
                    cardType(cardType == null ? "未定义" : cardType.getName());

            Common userStatus = commonMapper.selectById(item.getUserStatusId());
            employeeListItemBuilder.userStatusId(item.getUserStatusId()).
                    userStatus(userStatus == null ? "未定义" : userStatus.getName());

            Common position = commonMapper.selectById(item.getPositionId());
            employeeListItemBuilder.positionId(item.getPositionId()).
                    position(position == null ? "未定义" : position.getName());

            Common education = commonMapper.selectById(item.getEducationId());
            employeeListItemBuilder.educationId(item.getEducationId()).
                    education(education == null ? "未定义" : education.getName());

            return employeeListItemBuilder.build();
        }).collect(Collectors.toList());
        employeeListResponseBodyBuilder.items(employeeListItems);
        return employeeListResponseBodyBuilder.build();
    }
}
